﻿using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using MapSurface;
using OSMLib;
using WGS84;

namespace LaoMap
{
    class RenderOpenMapLibrary
    {
        public delegateReport _delegateReport;

        readonly OpenMapLibrary _lib;
        readonly Surface _surface;
        public RenderOpenMapLibrary(OpenMapLibrary lib, Surface surface)
        {
            _lib = lib;
            _surface = surface;
        }

        public void DoPlot(Canvas canvas)
        {
            for (int x = 0; x < _lib._regions.Count; x++)
            {
                var b = new SolidColorBrush {Opacity = 0.3};

                if (_lib._mapNames[x][5].StartsWith("Province"))
                {
                    b.Color = Colors.Green;
                }

                if (_lib._mapNames[x][5].StartsWith("Municipality"))
                {
                    b.Color = Colors.Brown;
                }

                if (_lib._mapNames[x][5].StartsWith("Special"))
                {
                    b.Color = Colors.Blue;
                }

                AddPolygon(_lib._regions[x], _lib._mapNames[x], b, canvas);

            }
        }

        void DebugOnEnter(object o, MouseEventArgs args)
        {
            if (_delegateReport == null)
                return;

            var p = o as Polyline;
            if (p == null)
                return;
            var names = p.Tag as string[];
            if (names == null)
                return;

            foreach (string t in names)
            {
                _delegateReport("" + t);
            }

            _delegateReport("++++++++++++++++++");
        }

        private void AddPolygon(IEnumerable<Wgs84Coordinate> n, IEnumerable names, SolidColorBrush b, Canvas canvas)
        {
            var sb = new SolidColorBrush {Opacity = 0.6, Color = b.Color};

            var polly = new Polyline
                            {
                                StrokeThickness = 1,
                                Fill = b,
                                Points = new PointCollection(),
                                StrokeStartLineCap = PenLineCap.Round,
                                StrokeEndLineCap = PenLineCap.Round,
                                StrokeLineJoin = PenLineJoin.Round,
                                Stroke = sb
                            };

            // polly.MouseDown += new MouseButtonEventHandler(ElMouseDown);

            polly.MouseEnter += DebugOnEnter;

            polly.Tag = names;

            foreach (Wgs84Coordinate n1 in n)
            {
                double px = _surface.GetPx(n1.Longitude);
                double py = _surface.GetPy(n1.Latitude);
                polly.Points.Add(new Point(px, py));
            }

            if (polly.Points.Count != 0)
                canvas.Children.Add(polly);
        }
    }
}
